﻿<style>
    .model-form .layui-form-label {
        width: 100px;
    }
    .model-form .layui-input-block {
        margin-left: 130px;
    }
</style>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar" lay-filter="userTbSearchForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">Account:</label>
                        <div class="layui-input-inline">
                            <input name="UserName" class="layui-input" placeholder=""/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">User Name:</label>
                        <div class="layui-input-inline">
                            <input name="RealName" class="layui-input" placeholder=""/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">Gender:</label>
                        <div class="layui-input-inline">
                            <select name="Gender">
                                <option value="">All</option>
                                <option value="1">Male</option>
                                <option value="0">Female</option>
                                <option value="2">Unknown</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="userTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>Search
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="userTable" lay-filter="userTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="userTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">Edit</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">Delete</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="reset">ResetPwd</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="userTbState">
    <input type="checkbox" lay-filter="userTbStateCk" value="{{d.UserId}}" lay-skin="switch"
           lay-text="正常|锁定" {{d.state==0?'checked':''}} style="display: none;"/>
    <p style="display: none;">{{d.state==0?'正常':'锁定'}}</p>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="userEditDialog">
    <form id="userEditForm" lay-filter="userEditForm" class="layui-form model-form">
        <input name="userId" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">Account:</label>
            <div class="layui-input-block">
                <input name="UserName" placeholder="" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">User Name:</label>
            <div class="layui-input-block">
                <input name="RealName" placeholder="" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">Gender:</label>
            <div class="layui-input-block">
                <input type="radio" name="Gender" value="1" title="Male"/>
                <input type="radio" name="Gender" value="0" title="Female"/>
                <input type="radio" name="Gender" value="2" title="Unknown" checked/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">Phone:</label>
            <div class="layui-input-block">
                <input name="PhoneNumber" placeholder="" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">Roles:</label>
            <div class="layui-input-block">
                <div id="userEditRoleSel"></div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="userEditSubmit" lay-submit>Save</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">Cancel</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'util', 'setter', 'admin', 'xmSelect'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        let setter = layui.setter;
        var admin = layui.admin;
        var xmSelect = layui.xmSelect;
        form.render('select', 'userTbSearchForm');

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#userTable',
            //url: layui.setter.baseServer + 'user.json',
            url: layui.setter.appServer + 'sys_user/getlist',
            method: 'post',
            headers: { 'Authorization': setter.getToken() },
            contentType: 'application/json',
            page: {
                limit: 50,
                limits: [20, 50, 100, 500],
                groups: 10
            },
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>Add New</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>Delete</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                { field: 'UserName', title: 'Account', minWidth: 150, sort: true},
                { field: 'RealName', title: 'User Name', minWidth: 150, sort: true},
                { field: 'Gender', title: 'Gender', sort: true},
                { field: 'PhoneNumber', title: 'Phone', minWidth: 150, sort: true},
                {
                    field: 'RoleIds', title: 'Roles', minWidth: 150, templet: function (d) {
                        return d.RoleList==null?'': d.RoleList.map(function (item) {
                            return '<span class="layui-badge layui-badge-gray">' + item.RoleName + '</span>';
                        }).join('&nbsp;&nbsp;');
                    }, sort: true, minWidth: 150
                },
                {
                    field: 'CreateTime', title: 'CreateTime',minWidth: 180, templet: function (d) {
                        return util.toDateString(d.CreateTime);
                    }, sort: true
                },
                { field: 'UserStatus', title: 'Account Status', templet: '#userTbState', sort: true, minWidth: 150},//#??待会儿再调这个状态。
                { title: 'Operate', toolbar: '#userTbBar', align: 'center', minWidth: 220, fixed: 'right'}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(userTbSearch)', function (data) {
            data.field.Gender = data.field.Gender != "" ? Number(data.field.Gender) : null;
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(userTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            } else if (obj.event === 'reset') { // 重置密码
                resetPsw(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(userTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('userTable');
                if (checkRows.data.length === 0) {
                    layer.msg('Please select the data to delete.', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.Id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? 'Edit' : 'Add') + ' User',
                content: $('#userEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('userEditForm', mData);
                    // 表单提交事件
                    form.on('submit(userEditSubmit)', function (data) {
                        data.field.Id = mData ? mData.Id : 0;
                        data.field.RoleIds = insRoleSel.getValue('valueStr');
                        data.field.Gender = Number(data.field.Gender||2)
                        var loadIndex = layer.load(2);
                        admin.req(layui.setter.appServer + 'sys_user/saveform', JSON.stringify(data.field), function (res) {
                            layer.close(loadIndex);
                            if (res.code === 0) { //$??这里确定下，请求是否加上了Token，以及是否需要 登录过期转到登录。
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'post');  // 实际项目可以是mData?'put':'post'
                        return false;
                    });
                    // 渲染多选下拉框 $??这里带调用接口处理。
                    var insRoleSel = xmSelect.render({
                        el: '#userEditRoleSel',
                        name: 'userEditRoleSel',
                        layVerify: 'required',
                        layVerType: 'tips',
                        data: [{
                            name: '管理员',
                            value: 1
                        }, {
                            name: '普通用户',
                            value: 2
                        }, {
                            name: '游客',
                            value: 3
                        }]
                    });
                    // 回显选中角色
                    if (mData && mData.RoleList) {
                        insRoleSel.setValue(mData.RoleList.map(function (item) {
                            return item.Id;
                        }));
                    }
                    // 禁止弹窗出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('Are you sure you want to delete the selected data ?', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req(layui.setter.appServer + 'sys_user/delete', {
                    id: obj.data ? obj.data.userId : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'post');
            });
        }

        /* 修改用户状态 */
        form.on('switch(userTbStateCk)', function (obj) {
            var loadIndex = layer.load(2);
            admin.req('ok.json', {
                userId: obj.elem.value,
                state: obj.elem.checked ? 0 : 1
            }, function (res) {
                layer.close(loadIndex);
                if (res.code === 200) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'get');
        });

        /* 重置密码 */
        function resetPsw(obj) {
            layer.confirm('Be sure to reset ' + obj.data.RealName + "'s login password ？", {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req('ok.json', {
                    userId: obj.data.userId
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 200) {
                        layer.msg(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'get');
            });
        }

    });
</script>
